import {Component, OnInit, ElementRef} from '@angular/core';
import {Router} from '@angular/router';
import {Authenticate} from '../libs/authenticate';
import {Global} from '../libs/global';
import {BaseComponent} from '../BaseComponent';
import {Http, Response, Headers} from '@angular/http';
import {HTTPService} from '../libs/HTTPService';
import {HttpExt} from '../libs/HttpExt';
@Component({
    selector: 'login',
    templateUrl: "app/login/login.html",
    host: {
        "(keypress)": "eventHandler($event)",   
        "class": "Host"
    }
})

export class Login extends BaseComponent implements OnInit {
    _Authenticate: Authenticate;
    constructor(private element: ElementRef, http:HttpExt,private router: Router,private httpService:HTTPService) {   
        super(http);
        this._Authenticate = new Authenticate(http);
    }

    ngOnInit() {
        this.authenticate();
        this._Authenticate.getExternalProvidersList();
    }

    login() {
        if(this.Validate()){
            this._Authenticate.login("","")
            this.router.navigate(['/home']);
            
        }
    }

    signup(event:Event) {
        event.preventDefault();
    }

    eventHandler(event:KeyboardEvent) {
        var keynum:number;
        if (window.event) { // IE
            keynum = event.keyCode;
        } else if (event.which) { // Netscape/Firefox/Opera
            keynum = event.which;
        }
        if (keynum == 13) {
            this.login();
        }
    }

    authenticate() {
        var hash = window.location.hash;
        if (hash.indexOf('#url') == 0) {
            window.location.hash = hash.replace('#url=', '');
        }
        else if (hash.indexOf('#access_token') == 0) {
            var hashValues = hash.split('=');

            this._Authenticate.setToken(hashValues[2].split('&')[0], hashValues[1].split('&')[0]);
            window.location.hash = "/";
        }
    }
    
    

} 

